package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    SetmealDao setmealDao;


    /**
     * reportData:{
         reportDate:null,  统计日期（今天）
         todayNewMember :0,  今日新增会员数
         totalMember :0,     总会员数
         thisWeekNewMember :0,  本周新增会员数,  本周一以后新增的会员数
         thisMonthNewMember :0,  本月新增会员数 ， 本月1号以后新增会员数
         todayOrderNumber :0,     今日预约数（有多少人预约了今天体检）(今天应该来多少人体检)
         todayVisitsNumber :0,     今日到诊数（今天检查了多少人）（今天实际来了多少人）
         thisWeekOrderNumber :0,   本周预约数（周一到周日应该来多少）
         thisWeekVisitsNumber :0,  本周到诊数（周一到今天为止实际来了多少人）
         thisMonthOrderNumber :0,   本月预约数（本月1号到月末应该来多少人）
         thisMonthVisitsNumber :0,  本月到诊数（本月到今天为止实际来了多少人）
         hotSetmeal :[  热门套餐（查询前三个套餐）
             {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
             {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
         ]
       }
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //获取今天
        String todayDate  = DateUtils.parseDate2String(new Date());
        //获取本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本周日的日期
        String thisWeekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        //获取本月1号的日期
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取本月最后一天的日期
        String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());


        //reportDate 统计时间
        String reportDate = todayDate;
        //todayNewMember :  今日新增会员数
        long todayNewMember = memberDao.findCountByDate(todayDate);
        //totalMember 总会员数
        long totalMember = memberDao.findTotalCount();
        //thisWeekNewMember 本周新增会员数
        long thisWeekNewMember = memberDao.findCountAfterDate(thisWeekMonday);
        //thisMonthNewMember  本月新增会员数
        long thisMonthNewMember = memberDao.findCountAfterDate(thisMonthFirstDay);
        //todayOrderNumber : 今日预约人数(预约今天要来的人数)
        long todayOrderNumber = orderDao.findTodayOrderNumber(todayDate);
        //todayVisitsNumber 今日到诊数
        long todayVisitsNumber = orderDao.findTodayVisitsNumber(todayDate);
        //thisWeekOrderNumber 本周预约人数
        long thisWeekOrderNumber = orderDao.findOrderNumber(thisWeekMonday, thisWeekSunday);
        //thisWeekVisitsNumber : 本周的到诊人数
        long thisWeekVisitsNumber = orderDao.findVisitsNumber(thisWeekMonday);
        //thisMonthOrderNumber: 本月预约人数
        long thisMonthOrderNumber = orderDao.findOrderNumber(thisMonthFirstDay, thisMonthLastDay);
        //thisMonthVisitsNumber 本月到诊人数
        long thisMonthVisitsNumber = orderDao.findVisitsNumber(thisMonthFirstDay);
        //查询热门套餐
        List<Map<String,Object>> hotSetmeal = setmealDao.findHotSetmeal();

        //创建返回值map集合
        Map<String,Object> map = new HashMap<>();
        map.put ( "reportDate" , reportDate);
        map.put ( "todayNewMember" , todayNewMember);
        map.put ( "totalMember" , totalMember);
        map.put ( "thisWeekNewMember" , thisWeekNewMember);
        map.put ( "thisMonthNewMember" , thisMonthNewMember);
        map.put ( "todayOrderNumber" , todayOrderNumber);
        map.put ( "todayVisitsNumber" , todayVisitsNumber);
        map.put ( "thisWeekOrderNumber" , thisWeekOrderNumber);
        map.put ( "thisWeekVisitsNumber" , thisWeekVisitsNumber);
        map.put ( "thisMonthOrderNumber" , thisMonthOrderNumber);
        map.put ( "thisMonthVisitsNumber" , thisMonthVisitsNumber);
        map.put ( "hotSetmeal" , hotSetmeal);

        return map;
    }
}
